package com.janke.softballnexus.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.google.appengine.api.datastore.KeyFactory;
import com.janke.softballnexus.server.EMF;

@Entity
public class League {

	public static void deleteAllLeagues() {
		EntityManager em = entityManager();
		try {
			em.createQuery("DELETE FROM " + League.class.getSimpleName() + " l")
					.executeUpdate();
		} finally {
			em.close();
		}
	}

	public static final EntityManager entityManager() {
		return EMF.get().createEntityManager();
	}

	@SuppressWarnings("unchecked")
	public static List<League> findAllLeagues() {
		EntityManager em = entityManager();
		try {
			List<League> list = em.createQuery(
					"SELECT l FROM " + League.class.getSimpleName() + " l")
					.getResultList();
			list.size();
			return list;
		} finally {
			em.close();
		}
	}

	public static League findLeague(Long id) {
		if (id == null) {
			return null;
		}
		EntityManager em = entityManager();
		try {
			League league = em.find(League.class,
					KeyFactory.createKey(League.class.getSimpleName(), id));
			return league;
		} finally {
			em.close();
		}
	}

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@NotNull
	@Size(min = 3, max = 15, message = "A league name must be no less than 3 characters long, and no more than 15 characters long")
	private String name;

	@NotNull
	@Size(min = 3, max = 25, message = "A hometown name must be no less than 3 characters long, and no more than 25 characters long")
	private String hometown;

	@Version
	@Column(name = "version")
	@NotNull
	@DecimalMin("0")
	private Integer version = 0;

	public League() {
	}

	public String getHometown() {
		return hometown;
	}

	public Long getId() {
		return id;
	}
	

	public String getName() {
		return name;
	}

	public Integer getVersion() {
		return version;
	}

	public void persist() {
		EntityManager em = entityManager();
		try {
			em.persist(this);
		} finally {
			em.close();
		}
	}

	public void remove() {
		EntityManager em = entityManager();
		try {
			em.remove(this);
		} finally {
			em.close();
		}
	}

	public void setHometown(String hometown) {
		this.hometown = hometown;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	@Override
	public String toString() {
		return "League " + name + " (" + hometown + ")";
	}

}
